/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.library.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.library.common.base.ApiResult;
import com.library.common.entity.MyPage;
import com.library.entity.CollectEntity;
import com.library.entity.ResourceEntity;
import com.library.service.CollectService;
import com.library.service.ResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 收藏
 *
 * @author pig
 * @date 2023-11-15 14:19:29
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("wxApi/collect" )
@Api(tags = "收藏管理", value = "收藏管理接口")
public class CollectController {

    @Resource
    private CollectService collectService;

    @Resource
    private ResourceService resourceService;

    /**
     * 通过id查询收藏
     * @param id id
     * @return ApiResult
     */
    @ApiOperation(value = "通过id查询")
    @GetMapping("/{id}" )
    public ApiResult getById(@PathVariable("id" ) Long id) {
        return ApiResult.ok(collectService.getById(id));
    }

    /**
     * 新增收藏
     * @param collect 收藏
     * @return ApiResult
     */
    @ApiOperation(value = "新增收藏")
    @PostMapping("add")
    public ApiResult save(@RequestBody CollectEntity collect) {
        LambdaQueryWrapper<CollectEntity> wrapper = Wrappers.lambdaQuery(collect);
        List<CollectEntity> list = collectService.list(wrapper);
        if (list.isEmpty()){
            return ApiResult.ok(collectService.save(collect));
        }else {
            return ApiResult.ok("");
        }

    }

    /**
     * 修改收藏
     * @param collect 收藏
     * @return ApiResult
     */
    @ApiOperation(value = "修改收藏")
    @PostMapping("edit")
    public ApiResult updateById(@RequestBody CollectEntity collect) {
        return ApiResult.ok(collectService.updateById(collect));
    }

    /**
     * 通过id删除收藏
     * @param ids id列表
     * @return ApiResult
     */

    @ApiOperation(value = "通过id删除收藏")
    @PostMapping("removeById")
    public ApiResult removeById(@RequestBody Long[] ids) {
        return ApiResult.ok(collectService.removeBatchByIds(CollUtil.toList(ids)));
    }



    @GetMapping("getUserResource/{openId}/{dataTypeId}")
    public ApiResult getUserResource(@PathVariable("openId" ) String openId,@PathVariable("dataTypeId" ) Long dataTypeId){
        return collectService.getUserResource(openId,dataTypeId);
    }

    @GetMapping("getOpenId/{code}")
    public ApiResult getOpenId(@PathVariable("code") String code){
        return collectService.getOpenId(code);
    }

    @GetMapping("getCollect/{openId}/{resourceId}")
    public String getCollect(@PathVariable("openId" ) String openId,@PathVariable("resourceId" ) Long resourceId){
        return collectService.getCollect(openId,resourceId);
    }


    @GetMapping("/remove/{id}" )
    public ApiResult remove(@PathVariable("id" ) Long id) {
        LambdaQueryWrapper<CollectEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(CollectEntity::getResourceId,id);
        return ApiResult.ok(collectService.remove(wrapper));
    }

    /**
     * 收藏列表
     */
    @ApiOperation("收藏列表")
    @PostMapping(value = "/list")
    public ApiResult<List<ResourceEntity>> list(@RequestBody CollectEntity param) {
        LambdaQueryWrapper<CollectEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CollectEntity::getOpenId, param.getOpenId());
        queryWrapper.eq(CollectEntity::getDelFlag, "0");
        queryWrapper.eq(CollectEntity::getDataTypeId, "1719628593258037250");
        List<CollectEntity> list = collectService.list(queryWrapper);
        List<ResourceEntity> resourceList = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                LambdaQueryWrapper<ResourceEntity> queryWrapper1 = new LambdaQueryWrapper<>();
                queryWrapper1.eq(ResourceEntity::getResourceId, list.get(i).getResourceId());
                ResourceEntity one = resourceService.getOne(queryWrapper1);
                if (one != null) {
                    one.setCollectId(list.get(i).getId());
                    resourceList.add(one);
                }
            }
        }


        return ApiResult.ok(resourceList);
    }
}
